package utils;

import java.util.LinkedList;
import java.util.List;

import fi.foyt.foursquare.api.FoursquareApi;
import fi.foyt.foursquare.api.FoursquareApiException;
import fi.foyt.foursquare.api.Result;
import fi.foyt.foursquare.api.entities.CompactVenue;
import fi.foyt.foursquare.api.entities.VenuesSearchResult;


public class FourSquareDAO {
	
	private String clientID = "I1JFXBJ3ZWZLXL0QRNMHKEI5ECVZPXPJUMLLK423K5KXRYBX";
	private String clientSecret = "2HJAKMNZI5SAFCC1WSZ55YXU0BMUDPZ54AS4QH20TWAOMROV";
	private String callBackUrl = "http://localhost:8080/";
	
	private static FourSquareDAO singleton;	
	public synchronized static FourSquareDAO fetchDAO(){
		if(singleton == null){
			singleton = new FourSquareDAO();
		}
		return singleton;
	}
	
	
	public List<String> searchVenues(String ll) throws FoursquareApiException {
	    // First we need a initialize FoursquareApi. 
	    FoursquareApi foursquareApi = new FoursquareApi(clientID, clientSecret, callBackUrl);
	    final List<String> ret = new LinkedList<String>();
	    // After client has been initialized we can make queries.
	    Result<VenuesSearchResult> result = foursquareApi.venuesSearch(ll, null, null, null, null, null, null, null, null, null, null);
	    
	    if (result.getMeta().getCode() == 200) {
	      // if query was ok we can finally we do something with the data
	      for (CompactVenue venue : result.getResult().getVenues()) {
	        // TODO: Do something we the data
	        ret.add(venue.getName());
	      }
	    } else {
	      // TODO: Proper error handling
	      System.out.println("Error occured: ");
	      System.out.println("  code: " + result.getMeta().getCode());
	      System.out.println("  type: " + result.getMeta().getErrorType());
	      System.out.println("  detail: " + result.getMeta().getErrorDetail()); 
	      return null;
	    }
	    
	    return ret;
	    
	  }
	
	
	
	

}
